package com.airconditioner.sales.view;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.Display;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.airconditioner.sales.R;
import com.airconditioner.sales.util.StringUtil;

/**
 * 作者：shuizi_wade on 2017/1/23 10:46
 * 邮箱：674618016@qq.com
 */
public enum InputHouseSizeDialog {
    INSTANCE;
    private Context context;
    private Dialog dialog;
    private Dialogcallback dialogcallback;
    private Button btn_sure;

    public void dialog(final Context con, final Dialogcallback dialogcallback) {
        this.context = con;
        dialog = new Dialog(context, R.style.dialog);
        dialog.setContentView(R.layout.dialog_input_house_size);
        // 获得当前窗体
        Window window = dialog.getWindow();
        // 重新设置
        WindowManager.LayoutParams lp = window.getAttributes();
        WindowManager m = ((Activity) context).getWindowManager();
        Display d = m.getDefaultDisplay();
        lp.x = 90; // 新位置X坐标
        lp.y = (int) context.getResources().getDimension(R.dimen.title_height); // 新位置Y坐标
        lp.alpha = 0.95f; // 透明度
        lp.width = d.getWidth();
        final EditText editText = (EditText) dialog.findViewById(R.id.dialog_et_input_size);
        editText.addTextChangedListener(textWatcher);
        btn_sure = (Button) dialog.findViewById(R.id.dialog_btn);
        btn_sure.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String content = editText.getText().toString().trim();
                if (StringUtil.isBlank(content))
                    Toast.makeText(con, con.getString(R.string.input_room_size), Toast.LENGTH_LONG).show();
                else {
                    dialogcallback.getSize(content);
                    dismiss();
                }
            }
        });
        dialog.show();
    }

    public interface Dialogcallback {
        void getSize(String content);
    }

    public void dismiss() {
        dialog.dismiss();
    }

    TextWatcher textWatcher = new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {

        }

        @Override
        public void afterTextChanged(Editable s) {
            if (StringUtil.isNotBlank(s.toString()))
                btn_sure.setBackgroundResource(R.drawable.btn_input_size_click);
            else
                btn_sure.setBackgroundResource(R.drawable.dialog_btn_click);
        }
    };
}
